package cn.itsource.org.service.impl;

import cn.itsource.basic.service.Impl.BaseServiceImpl;
import cn.itsource.org.mapper.DepartmentMapper;
import cn.itsource.org.domain.Department;
import cn.itsource.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService{

    /*@Autowired
    RedisTemplate redisTemplate;*/

    @Autowired
    DepartmentMapper departmentMapper;
    /*添加时不知道自己的id,需要完成添加后,返回自增id
                            1.选择自己的所属部门/2/5
            2.将/2/5作为dirPath提交给服务器
                            3.添加到数据库,但是缺少自己的id
                            4.拿到自增id 拼接到之前的/2/5
            5.发起修改  67  --->/2/5/67*/
    @Override
    public void add(Department department){
        super.add(department);
        this.update(department);
    }

    @Override
    public void update(Department department){
        department.setDirPath(department.getDirPath()+"/"+department.getId());
        super.update(department);
    }

    @Override
    public List<Department> queryByParentId(Long id) {
        return departmentMapper.queryByParentId(id);
    }


    @Override
    public List<Department> queryTree() {

        //查到所有部门
        List<Department> all = departmentMapper.loadAll();

        //构建Map,将List-all转为Map-all
        Map<Long, Department> map = new HashMap<>();
        for (Department department : all) {
            map.put(department.getId(),department);
        }

        //构建最终要返回的集合
        List<Department> result = new ArrayList<>();

        for (Department department : all) {
            Long pid = department.getParent_id();
            if (pid == null){
                result.add(department);
            }else {
                //父部门id找父部门对象
                Department parent = map.get(pid);
                //将自己加入父部门的children集合
                parent.getChildren().add(department);//parent.getChildren()--List<Department> children;
            }
        }
        return result;

//        //构建最终要返回的集合
//        List<Department> result = new ArrayList<>();
//        //尝试从缓存获取
//        Object tree = redisTemplate.opsForValue().get("tree");

        /*List result = (List)redisTemplate.opsForValue().get("tree");

        if (result==null){
            //查到所有部门
            List<Department> all = departmentMapper.loadAll();

            //构建Map,将List-all转为Map-all
            Map<Long, Department> map = new HashMap<>();
            for (Department department : all) {
                map.put(department.getId(),department);
            }

            result = new ArrayList<>();

            for (Department department : all) {
                Long pid = department.getParent_id();
                if (pid == null){
                    result.add(department);
                }else {
                    //父部门id找父部门对象
                    Department parent = map.get(pid);
                    //将自己加入父部门的children集合
                    parent.getChildren().add(department);//parent.getChildren()--List<Department> children;
                }
            }
            redisTemplate.opsForValue().set("tree",result);
        }
        return result;*/
    }

}
